package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huawei.hmsdemo10086.Adapter.AddressAdapter;
import com.huawei.hmsdemo10086.POJO.Address;
import com.huawei.hmsdemo10086.POJO.Goods;
import com.huawei.hmsdemo10086.POJO.Result;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddressActivity extends AppCompatActivity {

    /**
     * ListView对象
     */
    private ListView listView;
    private TextView mTitleTextView;
    private List<Address> datas;

    private AddressAdapter addressAdapter;
    private Button add_address;
    private Button back;

    private SharedPreferences sharedPreferences;
    private String userId;
    public static int tag = 0;
    public static Serializable cars;
    int flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        flag = 0;
        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userId = sharedPreferences.getString("userId",null);

        tag = getIntent().getIntExtra("tag",0);
        Log.d("tag", "tag1:"+tag);
        cars = (Serializable)getIntent().getSerializableExtra("cars");
        listView = (ListView)this.findViewById(R.id.address_list);
        mTitleTextView = (TextView) findViewById(R.id.text_title);
        mTitleTextView.setText("地址管理");
        back = (Button)findViewById(R.id.button_backward);
        datas = new ArrayList<Address>();
        add_address = (Button)findViewById(R.id.add_address);
        add_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(tag==1)
                {
                    Intent intent = new Intent(AddressActivity.this,Add_AddressActivity.class);
                    intent.putExtra("cars",(Serializable)getIntent().getSerializableExtra("cars"));
                    intent.putExtra("tag",1);
                    startActivity(intent);
                }
                else
                {
                    Intent intent = new Intent(AddressActivity.this,Add_AddressActivity.class);
                    startActivity(intent);
                }

            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(tag==1)
                {
                    Intent intent = new Intent(AddressActivity.this,MainActivity.class);
                    intent.putExtra("cars",(Serializable)getIntent().getSerializableExtra("cars"));
                    intent.putExtra("tag",1);
                    startActivity(intent);
                }
                else
                {
                    Intent intent = new Intent(AddressActivity.this,MainActivity.class);
                    startActivity(intent);
                }

            }
        });
        if(tag==1){
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Log.d("tag", "tag:"+tag);
                    Intent intent = new Intent(AddressActivity.this,PayActivity.class);
                    intent.putExtra("address",datas.get(position));
//                    intent.putExtra("goods",(Goods)getIntent().getSerializableExtra("goods"));
//                    intent.putExtra("num",getIntent().getIntExtra("num",0));
                    intent.putExtra("cars",(Serializable)getIntent().getSerializableExtra("cars"));
                    startActivity(intent);
                }
            });
        }

        getAddress();
        while(flag == 0)
        {
            System.out.println(1);
        }
        addressAdapter = new AddressAdapter(AddressActivity.this,datas);
        listView.setAdapter(addressAdapter);
        addressAdapter.notifyDataSetChanged();


    }

    private void getAddress()
    {
        String url = "http://10.0.2.2:8443/api/listAddress";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"userid\": \"" + userId + "\"\n" +
                "}";


        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                String rep = response.body().string();
                System.out.println(rep);

                Type type = new TypeToken<Result<List<Address>>>(){}.getType();
                Result<List<Address>> result = gson.fromJson(rep,type);

                for(int i = 0 ; i < result.getData().size() ; i++)
                {
                    System.out.println(result.getData().toString());
                }
                datas = result.getData();
                flag = 1;

            }
        });
    }
}